#encoding:utf8

import urlparse
import urllib
import requests
import json

try:
    import urlparse
    from urllib import urlencode
except: # For Python 3
    import urllib.parse as urlparse
    from urllib.parse import urlencode


class UrlParserApi(object):
    def __init__(self, url, auth_code):
        """
        用来请求urldb的wrapper

        :param url: domain:port/path1/path2...
        :param auth_code: as one parameter to add the tail of path
        """
        if not url.endswith("/"):
            url += "/"
        self.url = url
        self.auth_code = auth_code

        self.head_url = urlparse.urljoin(self.url, "opener/head/")
        self.source_url = urlparse.urljoin(self.url, "opener/source/")
        self.alexa_url = urlparse.urljoin(self.url, "opener/alexa/")

    def get_head(self, url, **kwargs):
        kwargs['url'] = url
        return self._request(self.head_url, kwargs)

    def get_source(self, url, **kwargs):
        kwargs['url'] = url
        return self._request(self.source_url, kwargs)

    def get_alexa_info(self, url, **kwargs):
        kwargs['url'] = url
        return self._request(self.alexa_url, kwargs)

    def _add_params(self, url, **kwargs):
        """
        给url的后面追加相关的参数, get方式
        """
        url_parts = list(urlparse.urlparse(url))
        query = dict(urlparse.parse_qsl(url_parts[4]))
        query.update(kwargs)
        url_parts[4] = urlencode(query)
        return urlparse.urlunparse(url_parts)
    #
    # def _request(self, url, data):
    #     resp = requests.get(url, data)
    #     page = resp.content
    #
    #     return page

    def _request(self, url, data={}):
        data = self._wrap_auth_code(data)
        resp = requests.post(url, data)
        resp = json.loads(resp.content)
        return resp

    def _wrap_auth_code(self, data):
        data["auth"] = self.auth_code
        return data

    def login(self):
        raise NotImplementedError("login api of remore opener is not implemented")


def test():
    from bs4 import BeautifulSoup as BS
    import pdb
    import json
    url = "http://www.baidu.com"
    upapi = UrlParserApi("http://ikan.space:9001/", "auth_code")
    content = upapi.get_head(url)
    print content
    data = content['data']['title']
    print data


if __name__ == "__main__":
    test()
